<?php
session_start();
include("../include/config.inc.php");
include("../include/version.inc.php");
include("../include/tables.inc.php");
include("../include/funktion.inc.php");
include("../include/rights.inc.php");


if(!isset($_SESSION['userid']))
   weiterleitung("index.php");

$rights = $_SESSION['ses_rights'];
$menu = menu($rights);

if(!check($_SERVER['PHP_SELF'],$rights))
  weiterleitung("uebersicht.php");

$fehler = "";
$was = $_GET['was'];
if(!is_numeric($id = $_GET['id'])) $id = 0;

if($was == 'save')
   {
   $text = $_POST['text'];
   $betreff = $_POST['betreff'];
   $modus = $_POST['modus'];

   $aendern = "UPDATE `$textdb` SET
   `betreff` = '$betreff',
   `text` = '$text',
   `modus` = '$modus'
   WHERE `name` = 'newsletter';";
   $update = mysql_query($aendern);
   }

if($was == 'save' AND isset($_POST['upload']) AND !empty($_FILES['datei']['name']))
   {
   $name = $_FILES['datei']['name'];
   $size = $_FILES['datei']['size'];
   $mime = $_FILES['datei']['type'];
   $value = chunk_split(base64_encode(file_get_contents($_FILES['datei']['tmp_name'])));

   $eintrag = "INSERT INTO `$anhangdb` (`name`, `mime`, `size`, `value`) VALUES ('$name', '$mime', '$size', '$value')";
$eintragen = mysql_query($eintrag) OR die(mysql_error());
   }

if($was == 'del')
   {
   $loeschen = "DELETE FROM `$anhangdb` WHERE `id` = '$id'";
   $loesch = mysql_query($loeschen);
   }


if($was == "newsletter_html")
   {
   $abfrage = "SELECT `modus`,`text` FROM `$textdb` WHERE `name` = 'newsletter';";
   $ergebnis = mysql_query($abfrage);
   $row = mysql_fetch_object($ergebnis);

   if($row->modus == "html")
      echo $row->text;
   else
      echo nl2br(htmlentities($row->text));
   exit;
   }
else if($was != "send")
   {
   $abfrage = "SELECT `betreff`,`text`,`modus` FROM `$textdb` WHERE `name` = 'newsletter';";
   $ergebnis = mysql_query($abfrage);
   $row = mysql_fetch_object($ergebnis);


   $email_betreff = htmlentities($row->betreff);
   $email_text = htmlentities($row->text);
   $modus = $row->modus;



   $anhang = "";
   $abfrage = "SELECT `id`,`name`,`size` FROM `$anhangdb` ORDER BY `id`;";
   $ergebnis = mysql_query($abfrage);
   while($row = mysql_fetch_object($ergebnis))
      {
      $size = $row->size;
      if($size > (1024*1024)) //MB
         $size = round(($size/(1024*1024)),2)." MB";
      else if($size > 1024) // LB
         $size = round($size/1024,2)." KB";
      else
         $size .= " Byte";


      $anhang .= "<a target=\"_blank\" href=\"anhang.php?id=$row->id\">$row->name</a> ($size)&nbsp;&nbsp;<a href=\"?was=del&id=$row->id\">L&ouml;schen</a><br>";
      }
   } //Newsletter bearbeiten
else
   {
   $abfrage = "SELECT `betreff`,`text`,`modus` FROM `$textdb` WHERE `name` = 'newsletter';";
   $ergebnis = mysql_query($abfrage);
   $row = mysql_fetch_object($ergebnis);


   $email_betreff = htmlentities($row->betreff);
   $email_text = "<iframe src=\"write.php?was=newsletter_html\" width=\"90%\" height=\"300\" name=\"Newsletter\" marginheight=\"5\" marginwidth=\"5\"><a href=\"write.php?was=newsletter_html\">Newsletter betrachten</a></iframe>
";
   $modus = $row->modus;

   $anhang = "";
   $abfrage = "SELECT `id`,`name`,`size` FROM `$anhangdb` ORDER BY `id`;";
   $ergebnis = mysql_query($abfrage);
   while($row = mysql_fetch_object($ergebnis))
      {
      $size = $row->size;
      if($size > (1024*1024)) //MB
         $size = round(($size/(1024*1024)),2)." MB";
      else if($size > 1024) // LB
         $size = round($size/1024,2)." KB";
      else
         $size .= " Byte";


      $anhang .= "<input type=\"Checkbox\" name=\"att$row->id\" value=\"1\" checked>&nbsp;&nbsp;<a target=\"_blank\" href=\"anhang.php?id=$row->id\">$row->name</a> ($size)&nbsp;&nbsp;<br>";
      }
   }

?>

<html>
<head>
<title>Clanletter - Admin Bereich</title>
<meta name="author" content="Andavos">
<style type="text/css">
<!--
a:hover { text-decoration:none; }

BODY {

     SCROLLBAR-FACE-COLOR: #4F5271;
     SCROLLBAR-HIGHLIGHT-COLOR: #243D62;
    SCROLLBAR-SHADOW-COLOR: rgb(32,32,32);
    SCROLLBAR-ARROW-COLOR: #ffffff;

}

.anhang {
 font-size: 14px;
 color: #000000;
 background-color: #899FBF;

 border-color: #000000;
 border-top-width :1pt;
 border-right-width : 1px;
 border-bottom-width : 1px;
 border-left-width : 1px;
}

-->
</style>


</head>
<body text="#000000" bgcolor="#D0D0D0" link="#000060" alink="#000060" vlink="#000060">


<table cellpadding="5" cellspacing="2" bgcolor="black" align="center" width="85%">
<tr>
 <td width="205" valign="top" bgcolor="#899FBF"><a target="_blank" href="http://www.php-einfach.de"><img src="php-einfach.jpg" width="199" height="66" border="0" alt="PHP-Einfach.de"></a> <br>


 <?php echo $menu; ?>


<font size="-1"> <a target="_blank" href="http://www.php-einfach.de">&copy; PHP-Einfach.de Clanletter 2.0</a><br>
 </td>
  <td valign="top" bgcolor="#9BAAC1">
<div align="center"><h2>Newsletter</h2></div>




<?php if($was == "send") { ?>
<form action="send.php" method="post" target="">
<a href="write.php"><b>Newsletter bearbeiten</b></a>
<br><br>
<b>Betreff:</b> <?php echo $email_betreff; ?><br><br>

<b>Text:</b><br>
<?php echo $email_text; ?> <br><br>

<b>Email-Art:</b> <?php echo $modus; ?><br><br>


<?php ($anhang =="") ? "" : print("<b>Dateianh&auml;nge:</b><br>"); ?>
<?php echo $anhang; ?>


<?php if($config['archiv'] == 1): ?>
<br>
<input type="Checkbox" name="archiv" value="1" id="archiv" checked><label for="archiv"> Im Archiv speichern</label>
<?php endif; ?>

<center><input type="Submit" name="" value="Newsletter versenden"></center>




</form>

<?php } else { ?>
<a href="?was=send"><b>Newsletter versenden</b></a>

<form action="?was=save" method="post" enctype="multipart/form-data">
<b>Betreff:</b><br>
<input type="Text" name="betreff" value="<?php echo $email_betreff; ?>" size="40" maxlength="150"><br><br>

<b>Text:</b><br>
<textarea name="text" cols="60" rows="15"><?php echo $email_text; ?></textarea><br><br>

<b>Email-Art:</b><br>
<input type="Radio" name="modus" value="text" id="text" <?php ($modus != "html") ? print("checked") : ""; ?>> <label for="text">Text</label><br>
<input type="Radio" name="modus" value="html" id="html" <?php ($modus == "html") ? print("checked") : ""; ?>> <label for="html">HTML (<small>Zeilenumbr&uuml;che mit &lt;br></small>)</label><br><br>

<b>Dateianhang:</b><br>
<input type="file" name="datei" size="20" class="anhang">
&nbsp;&nbsp;<input type="Submit" name="upload" value="Hochladen" class="anhang"><br><br>

<?php ($anhang =="") ? "" : print("<b>Bereits hochgeladen:</b><br>"); ?>
<?php echo $anhang; ?>


<br><br>
<center><input type="Submit" name="" value="Speichern">&nbsp;&nbsp;&nbsp;<input type="reset" onClick="return confirm('Wollen Sie wirklich den alten Text laden?')"></center>

</form>

<b>Um den Benutzer mit seinem Namen anzusprechen, muss an der gew&uuml;nschten Stelle {name} stehen.</b>



<?php } ?>




  </td>
</tr>
</table>

</body>
</html>
